
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  Red
//----

extern int nPreDays = 3;
extern int    Line.Width = 3; 
extern color  Line.Color     = Red; 

double MiddleBuf[];

int ExtCountedBars=0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void init()
  {   
   int    draw_begin;
 //---- drawing settings      
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, Line.Width, Line.Color);
//----  
   //draw_begin = 60;
   //SetIndexDrawBegin(0, draw_begin);
   
//---- indicator buffers mapping
   SetIndexBuffer(0, MiddleBuf);
      
   
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit() 
  {
//----
  }  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void start() 
{
   ExtCountedBars = IndicatorCounted();

//---- check for possible errors
   if (ExtCountedBars<0) return(-1);

   datetime dtTime= StrToTime(TimeToStr(iTime(NULL, PERIOD_D1,1), TIME_DATE) + " 23:59");
   int iBar = iBarShift(NULL, 0, dtTime);
  
//---- the last counted bar will be recounted
   if(ExtCountedBars > 0) 
       ExtCountedBars -= iBar;
   int limit = Bars - ExtCountedBars ;
 
//---- main loop 
   for(int i = 0; i < limit; i++) 
   {             
      dtTime= StrToTime(TimeToStr(Time[i], TIME_DATE) + " 00:00");
      iBar = iBarShift(NULL, PERIOD_D1, dtTime);           
      
       double high = iHigh(NULL,PERIOD_D1,iHighest(NULL, PERIOD_D1, MODE_HIGH, nPreDays, iBar+1));
       double low = iLow(NULL,PERIOD_D1,iLowest(NULL, PERIOD_D1, MODE_LOW, nPreDays, iBar+1));
       MiddleBuf[i] = (high + low)/2;       
   }
     
//---- done
 
   return(0);
}
//+------------------------------------------------------------------+